import { formatTime } from "../../utils/util.js"
const app = getApp()

Page({
    data: {
        modalVisible: false,
        banners: [
            '/images/index/banner1.jpg',
            '/images/index/banner3.jpg',
        ],
        orderList: [],
        tabsList: [
            {
                id: 0,
                value: "区域",
                isActive: true,
                isDesc: true,
                isSort: true,//是否显示三角下拉图标
                chooseFinish: true
            },
            {
                id: 1,
                value: "价格",
                isActive: false,
                isDesc: true,
                isSort: true,//是否显示三角下拉图标
                chooseFinish: true
            },
            {
                id: 2,
                value: "面积",
                isActive: false,
                isDesc: true,
                isSort: true,//是否显示三角下拉图标
                chooseFinish: true
            }, {
                id: 3,
                value: "更多",
                isActive: false,
                isSort: true,//是否显示三角下拉图标
                chooseFinish: true
            },
            {
                id: 4,
                value: "排序",
                isActive: true,
                isSort: true,//是否显示三角下拉图标
                chooseFinish: true
            }
        ],
     
        newSort: [{
            id: 1,
            value: "time",
            label: "按发布时间由新到旧",
            isChoose: true
        }, {
            id: 2,
            value: "timedesc",
            label: "按发布时间由旧到新",
            isChoose: false
        }, {
            id: 3,
            value: "asc",
            label: "按赏金从低到高",
            isChoose: false
        }, {
            id: 4,
            value: "desc",
            label: "按赏金从高到低",
            isChoose: false
        },
            // {
            //     id: 5,
            //     value: "",
            //     label:"面积从大到小排序",
            //     isChoose: false
            // },{
            //     id: 6,
            //     value: "",
            //     label:"面积从小到大排序",
            //     isChoose: false
            // },
        ],
        // 房屋用途选择
        usePro: [{
            id: 0,
            value: "全部",
            isChoose: true
        },
        {
            id: 1,
            value: "住宅",
            isChoose: false

        },
        {
            id: 3,
            value: "民宿",
            isChoose: false

        },
        {
            id: 4,
            value: "商业",
            isChoose: false

        },
        {
            id: 5,
            value: "工业",
            isChoose: false

        },
        {
            id: 6,
            value: "其他",
            isChoose: false

        },
        ],
        // 接口需要的参数
        QureyParams: {
            id: 1, //id=1为1-10条数据,2为11-20条,相当于页数pageNmu
            sort: "desc",
            areaPro: [0, 99999],
            usePro: ["不限"],
        },
        chooseFinish: true,

    },
    onLoad() {
        app.checkUserStatus()
    },
    async onShow() {
        const userInfo = wx.getStorageSync('userInfo')
        if (!userInfo) return this.setData({ modalVisible: true })
        this.getOrder()

    },

    // 获取全部订单列表根据openid
    async getOrder() {
        const res = await app.request({
            url: 'getOrder', method: "POST", data: { status: ['审核通过，待接单'] }
        })
        if (res.meta.status >= 300) return wx.showToast({ title: "没有订单呢", icon: 'error' });
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
        this.setData({ orderList: res.data.order })
    },
    getUserProfile(e) {
        const _this = this
        wx.getUserProfile({
            desc: '业务需要',
            success: (res) => {
                const userInfo = res.userInfo
                wx.setStorageSync('userInfo', userInfo)
                app.request({
                    url: `updateUser`, data: {
                        openid: app.openid,
                        ...userInfo,
                    }, method: "POST"
                })
                _this.setData({ modalVisible: false })
                wx.reLaunch({ url: '/pages/index/index' });
            },
            fail: (err) => {
                this.onRefuseInfo()
            }
        })
    },
    onRefuseInfo() {
        wx.showModal({ title: "提示", content: "取消授权将导致无法提供部分功能服务。请授权", showCancel: false })
    },
    onPullDownRefresh: function () {
        //调用刷新时将执行的方法
        this.getOrder();
    },
    // 子组件传过来的点击菜单标签事件
    handleTabsItemChange(e) {
        //console.log(e);
        const noMore = false;
        // 获取点击标签索引
        const { index } = e.detail;
        // 拷贝菜单数组tabsList 拷贝参数数组QureyParams
        var { tabsList, QureyParams, chooseFinish } = this.data;
        // QureyParams = {};

        // 切换逻辑
        tabsList.forEach((v, i) => {
            // 外层循环是为了切换标签isActive
            if (index === i) {
                // 内层循环为了改变参数逻辑
                if (v.isActive) {
                    // 如果原本就已经选中 改变排序
                    // v.isDesc = !v.isDesc;
                    v.chooseFinish = !v.chooseFinish
                }
                // 如果没有被选中
                else {
                    // 激活选中
                    v.isActive = true;
                    v.chooseFinish = false
                }
            } else {
                // 取消选中
                v.isActive = false;
                v.chooseFinish = true;
            }
        })
        this.setData({
            tabsList,
            QureyParams,
            // houseList: [],
            chooseFinish,
            noMore: false
        })
        //     // 获取搜索房源列表数据
        // this.getHouseList();
    },
    // 点击遮罩
    showFlag() {
        let { tabsList } = this.data;
        tabsList.forEach(v => {
            if (v.isActive) {
                v.chooseFinish = true;
            }
        });
        this.setData({
            tabsList
        })

    },
    //点击新排序
    newSortChange(e) {
        //console.log(e);
        const { value, index } = e.target.dataset;
        let { QureyParams, newSort } = this.data;
        newSort.forEach((v, i) => {
            index == i ? v.isChoose = true : v.isChoose = false
        });
        if (!value) {
            delete QureyParams.sort
            this.setData({
                QureyParams, newSort
            })
            return
        }
        QureyParams.sort = value
        this.setData({
            QureyParams, newSort,
        })
    },

    // 提交按钮
    async handleSubmit(e) {
        //console.log(e);
        const { index } = e.currentTarget.dataset;
        //console.log({ index });
        let { tabsList, QureyParams } = this.data;
        tabsList[index].chooseFinish = true;
        // 判断前后大小
        if (QureyParams.price) {
            if (QureyParams.price.length == 1) {
                delete QureyParams.price;
                await showToast({ title: "请输入完整自定义价格" });
                this.setData({
                    QureyParams
                })
                return;
            } if (QureyParams.price[0] > QureyParams.price[1]) {
                let temp = QureyParams.price[0];
                QureyParams.price[0] = QureyParams.price[1];
                QureyParams.price[1] = temp;
            }
            // 改变tabs表头
            tabsList[1].value = QureyParams.price[0] + "~" + QureyParams.price[1];
        }

        if (QureyParams.areaPro) {
            if (QureyParams.areaPro.length == 1) {
                delete QureyParams.areaPro;
                await showToast({ title: "请输入完整自定义面积" });
                this.setData({
                    QureyParams
                })
                return;
            } if (QureyParams.areaPro && QureyParams.areaPro.length == 2 && QureyParams.areaPro[0] > QureyParams.areaPro[1]) {
                let temp = QureyParams.areaPro[0];
                QureyParams.areaPro[0] = QureyParams.areaPro[1];
                QureyParams.areaPro[1] = temp;
            }
            // 改变tabs表头
            tabsList[2].value = QureyParams.areaPro[0] + "~" + QureyParams.areaPro[1];
        }

        this.setData({
            tabsList, QureyParams,
            chooseFinish: true
        })
        const { orderList } = this.data
        if (QureyParams.sort === 'desc') orderList.sort((a, b) => b.bounty - a.bounty)
        if (QureyParams.sort === 'asc') orderList.sort((a, b) => a.bounty - b.bounty)
        if (QureyParams.sort === 'timedesc')  orderList.sort((a, b) => new Date(a.create_at).getTime() -  new Date(b.create_at).getTime() )
        if (QureyParams.sort === 'timeasc') orderList.sort((a, b) => new Date(b.create_at).getTime() -  new Date(a.create_at).getTime() )
        this.setData({ orderList })
    },
})